package com.bw.service.impl;

import com.bw.dao.SupplierDao;
import com.bw.dao.impl.SupplierDaoImpl;
import com.bw.pojo.Supplier;
import com.bw.pojo.vo.SupplierVo;
import com.bw.service.SupplierService;
import com.bw.util.MyBatisUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.List;

public class SupplierServiceImpl implements SupplierService {
    SqlSessionFactory sqlSessionFactory = MyBatisUtil.getSqlSessionFactory();
    SupplierDaoImpl dao = new SupplierDaoImpl();
    @Override
    public PageInfo<SupplierVo> getSupplierList(SupplierVo supplierVo) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        SupplierDao supperMapper = sqlSession.getMapper(SupplierDao.class);
        PageHelper.startPage(supplierVo.getPageNum(), supplierVo.getPageSize());
        List<SupplierVo> list = supperMapper.getSupplierList(supplierVo);
        PageInfo<SupplierVo> supplierVoPageInfo = new PageInfo<>(list);
        sqlSession.close();
        return supplierVoPageInfo;
    }

    @Override
    public List<Supplier> getSupplier() {
        return dao.getSupplier();
    }

    @Override
    public void getSupplierDel(SupplierVo supplierVo) {
        dao.getSupplierDel(supplierVo.getUid());
    }

    @Override
    public void supplierDelAll(String ids) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        SupplierDao supperMapper = sqlSession.getMapper(SupplierDao.class);
        String[] split = ids.split(",");
        supperMapper.supplierDelAll(split);
        sqlSession.commit();
        sqlSession.close();
    }

    @Override
    public void supplierAdd(List<Supplier> list) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        SupplierDao supperMapper = sqlSession.getMapper(SupplierDao.class);
        supperMapper.supplierAddAll(list);
        sqlSession.commit();
        sqlSession.close();
    }

}
